package com.naizhuai.ui.msgmanager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.MultStateView;
import com.naizhuai.ui.components.swipeback.SwipeBackActivity;
import com.naizhuai.ui.entity.User.AppMessage;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

public class MsgManagerActivity extends SwipeBackActivity {
    @BindView(R.id.msg_classify)
    RecyclerView msgClassify;
    MsgDataAdapter mDataAdapter;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<AppMessage> oldDatas;

    public static void start(Context context) {
        Intent intent = new Intent(context, MsgManagerActivity.class);
        if (context instanceof BaseActivity)
            ((BaseActivity) context).startActivityTransition(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar.setNavigationOnClickListener(v -> finish());
        msgClassify.setLayoutManager(new LinearLayoutManager(this));
        msgClassify.setAdapter(mDataAdapter = new MsgDataAdapter());
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(refreshlayout ->
                loadMessage()
        );
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.autoRefresh();
    }

    private void loadMessage() {
        UserManageDataApiStoreIml.getInstance().getAppMessage()
                .subscribe(new NetWorkObserver<List<AppMessage>>() {
                    @Override
                    protected void onFail(String errormsg) {
                        if (refreshLayout!=null){
                            refreshLayout.finishRefresh();
                        }
                         shortToast(errormsg);
                    }
                    @Override
                    protected void onSuccess(List<AppMessage> result) {
                        if (refreshLayout!=null){
                            refreshLayout.finishRefresh();
                        }
                        if (oldDatas == null) {
                            oldDatas = result;
                            mDataAdapter.setResult(result);
                            mDataAdapter.notifyDataSetChanged();
                        } else {
                            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(oldDatas, result), true);
                            diffResult.dispatchUpdatesTo(mDataAdapter);
                            mDataAdapter.setResult(result);
                            oldDatas = result;
                        }
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_msg_manager;
    }

    class DiffCallBack extends DiffUtil.Callback {
        private List<AppMessage> mOldDatas, mNewDatas;

        public DiffCallBack(List<AppMessage> mOldDatas,
                            List<AppMessage> mNewDatas) {
            this.mOldDatas = mOldDatas;
            this.mNewDatas = mNewDatas;
        }

        @Override
        public int getOldListSize() {
            return mOldDatas != null ? mOldDatas.size() : 0;
        }

        @Override
        public int getNewListSize() {
            return mNewDatas != null ? mNewDatas.size() : 0;
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
            return mOldDatas.get(oldItemPosition).messageContent
                    .equals(mNewDatas.get(newItemPosition).messageContent);
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            return false;
        }
    }
}
